import { deepClone } from './index'
/**
 * 作者: 致心
 * 创建时间: 2020-08-19 10:01
 **/
function _beginNumberLastNumberPPOrLess(beginNumber = '1', POL = false) {
  if (beginNumber.length > 1) {
    const strings = beginNumber.split('-')
    beginNumber = ''
    let number = Number(strings[strings.length - 1])
    if (POL) {
      number++
    } else {
      number--
    }
    strings[strings.length - 1] = String(number)
    strings.forEach(value => {
      beginNumber += (value + '-')
    })
    beginNumber = beginNumber.substring(0, beginNumber.lastIndexOf('-'))
  } else {
    if (POL) {
      beginNumber = String(++beginNumber)
    } else {
      beginNumber = String(--beginNumber)
    }
  }
  return beginNumber
}

/**
 * @Description: 这是转换以后返回的结果，结果里面保留list的所有数据，在用
 * 自己传进去的identifier作为标识去区分节点，比如：
 * 父节点为1
 * 子节点就是1-1,1-2,1-3的形式，以此类推
 * @Author: 致心
 * @Date: 2020/8/19 10:14
 */
function _listTree2List(tree = [], changeSource = false, field = '', identifier = '', beginNumber = '1') {
  if (Object.prototype.toString.call(tree) !== '[object Array]') {
    throw new Error('数据类型错误')
  }
  const result = []
  tree.forEach(value => {
    const resultItem = {}
    Object.keys(value).forEach(value1 => {
      if (value1 !== field) {
        resultItem[value1] = value[value1]
      }
    })
    resultItem[identifier] = beginNumber
    if (changeSource) {
      value[identifier] = beginNumber
    }
    beginNumber = _beginNumberLastNumberPPOrLess(beginNumber, true)
    result.push(resultItem)
    if (value.hasOwnProperty(field) && value[field] && value[field].length > 0) {
      result.push(..._listTree2List(value[field], changeSource, field, identifier, _beginNumberLastNumberPPOrLess(beginNumber, false) + '-1'))
    }
  })
  return result
}

/**
 * @Description: 把树形list的转换成一个list
 * @param tree
 * @param changeSource  默认： false 是否在源树里面加入唯一标识
 * @param field         默认： children 传入属性名，树形list对象里面这个属性也为一个list
 * @param identifier    默认： childrenIndex 作为标识去区分节点，比如：父节点为1,子节点就是1-1,1-2,1-3的形式，以此类推
 * @Author: 致心
 * @Date: 2020/8/19 11:55
 */
export function listTree2List(tree = [], changeSource = false, field = 'children', identifier = 'childrenIndex') {
  if (field === '' || field.length < 1) {
    throw new Error('field数据错误')
  }
  if (identifier === '' || identifier.length < 1) {
    throw new Error('identifier数据错误')
  }
  return _listTree2List(tree, changeSource, field, identifier)
}

/**
 * @Description :
 * @Author :致心
 * @Date :2020/12/30 15:55
 * @param tree        { Array   } 传入源数组或者是已经被这个文件里面的方法转化过的数组
 * @param source      { Object  } 传入要查找父对象的子对象
 * @param grand       { Boolean } 是否查找曾级
 * @param isEqField   { Array   } 判断对象是否相等的依据字段，因为会先从树里面找到我们的对应的source，然后在找父对象，所以传入的字段必须在source里面要有
 * @param field       { String  } 树下面子数组的属性
 * @param identifier  { String  } 查找的标识
 * @returns           { []|*    }
 */
export function findParent(tree = [], source = {}, grand = false, isEqField = ['id'], field = 'children', identifier = 'childrenIndex') {
  if (Object.prototype.toString.call(source) !== '[object Object]') {
    throw new Error('source数据错误')
  }
  for (let i = 0; i < isEqField.length; i++) {
    const item = isEqField[i]
    if (Object.keys(source).findIndex(j => j === item) === -1) {
      throw new Error('isEqField数据错误')
    }
  }
  let list = []
  if (!tree.hasOwnProperty(identifier)) {
    list = listTree2List(tree, false, field, identifier)
  } else {
    list = JSON.parse(JSON.stringify(tree))
  }
  const find = list.find(i => {
    let count = 0
    isEqField.forEach(j => {
      if (i[j] === source[j]) {
        count++
      }
    })
    return count === isEqField.length
  })
  if (!find) {
    throw new Error('source数据错误')
  }
  let number = find[identifier].lastIndexOf('-')
  let s = find[identifier].substring(0, number)
  const result = []
  while (s) {
    if (grand) {
      result.unshift(list.find(i => i[identifier] === s))
      number = s.lastIndexOf('-')
      s = s.substring(0, number)
    } else {
      result.push(list.find(i => i[identifier] === s))
      break
    }
  }
  return result
}

/**
 * @Description :
 * @Author      :致心
 * @Date        :2021/2/20   16:41
 * @param tree        { Array   } 传入源数组或者是已经被这个文件里面的方法转化过的数组
 * @param source      { Object  } 传入要查找父对象的子对象
 * @param grand       { Boolean } 是否查找曾级
 * @param isEqField   { Array   } 判断对象是否相等的依据字段，因为会先从树里面找到我们的对应的source，然后在找父对象，所以传入的字段必须在source里面要有
 * @param field       { String  } 树下面子数组的属性
 * @param identifier  { String  } 查找的标识
 * @returns           { []|*    }
 */
export function findParentAndDelIdentifier(tree = [], source = {}, grand = false, isEqField = ['id'], field = 'children', identifier = 'childrenIndex') {
  const result = findParent(tree, source, grand, isEqField, field, identifier)
  result.forEach(i => {
    delete i[identifier]
  })
  return result
}
/**
 *
 * @param {*} treeData 树形源数据
 * @param {*} selectData 选中数据
 * @param {*} params ID对象
 * @param {*} paramsName  名称对象
 * @param {*} typeNum 类型
 * @returns []
 */
export function getSelectData(treeData, selectData, params, paramsName, typeNum) {
  const selectDataArr = []
  const arr = this.getSelectTreeArr(selectData)
  arr.forEach(i => {
    const find = treeData
    find.push(i)
    const paramsF = Object.keys(params)
    for (let i = 1; i < find.length; i++) {
      params[paramsF[i - 1]] = find[i].id
    }
    const paramsH = Object.keys(paramsName)
    for (let i = 1; i < find.length; i++) {
      paramsName[paramsH[i - 1]] = find[i].name
    }
    const o = {}
    Object.assign(o, params, paramsName, { 'workType': typeNum })
    selectDataArr.push(o)
  })
  return selectDataArr
}
// 数据过滤 arr为树形选中的数据
export function getSelectTreeArr(arr) {
  const obj = arr
  const mian = obj.filter(i => { return i.level === 0 })
  const projectObj = obj.filter(i => { return i.level === 1 })
  const buildObj = obj.filter(i => { return i.level === 2 })
  const unitObj = obj.filter(i => { return i.level === 3 })
  const floorObj = obj.filter(i => { return i.level === 4 })
  const roomObj = obj.filter(i => { return i.level === 5 })
  const roomObjArr = deepClone(roomObj)
  const floorObjArr = deepClone(floorObj)
  const unitObjArr = deepClone(unitObj)
  const buildObjArr = deepClone(buildObj)
  const projectObjArr = deepClone(projectObj)
  roomObjArr.forEach((i) => {
    const index = floorObj.findIndex((j) => {
      return i.floorId === j.id
    })
    if (index !== -1) {
      roomObj.splice(0, 1)
    }
  })
  floorObjArr.forEach((i) => {
    const index = unitObj.findIndex((j) => {
      return i.unitId === j.id
    })
    if (index !== -1) {
      floorObj.splice(0, 1)
    }
  })
  unitObjArr.forEach((i) => {
    const index = buildObj.findIndex((j) => {
      return i.buildId === j.id
    })
    if (index !== -1) {
      unitObj.splice(0, 1)
    }
  })
  buildObjArr.forEach((i) => {
    const index = projectObj.findIndex((j) => {
      return i.projectId === j.id
    })
    if (index !== -1) {
      buildObj.splice(0, 1)
    }
  })
  projectObjArr.forEach((i) => {
    const index = mian.findIndex((j) => {
      return i.organizationId === j.id
    })
    if (index !== -1) {
      projectObj.splice(0, 1)
    }
  })
  return [...mian, ...projectObj, ...buildObj, ...unitObj, ...floorObj, ...roomObj]
}
